//按钮相关操作的方法类
export default class HandleBtn {
  constructor(sweeper) {
    this.sweeper = sweeper
    this.init()
  }
  init(){
    let sweeper = this.sweeper
    this.xStart = 0
    this.xEnd = sweeper.difficulty.ux - 1
    this.yStart = 0
    this.yEnd = sweeper.difficulty.uy - 1
    this.zStart = 0
    this.zEnd = sweeper.difficulty.uz - 1
  }
  //撤销隐藏
  hideBack() {
    let hiddenList = this.sweeper.hiddenList
    if (hiddenList.length > 0) {
      hiddenList.pop().forEach(obj => {
        obj.visible = true
      })
      this.sweeper.musicCtrl.playGameAudio({type:'hidden'})
    }
  }
  //层级隐藏(direction隐藏方向xyz,reverse是否逆向)
  floorHidden(direction, reverse) {
    let group = this.sweeper.threeManager.group
    let mode = this.sweeper.mode
    if (!reverse) reverse = false
    let up,uq,bp,bq
    let o = 0
    switch (direction) {
      case 'x':
        if (this.xStart == this.xEnd) return
        if (reverse) {
          o = this.xStart
          this.xStart++
        } else {
          o = this.xEnd
          this.xEnd--
        }
        up = this.yEnd
        bp = this.yStart
        uq = this.zEnd
        bq = this.zStart
        break
      case 'y':
        if (this.yStart == this.yEnd) return
        if (reverse) {
          o = this.yStart
          this.yStart++
        } else {
          o = this.yEnd
          this.yEnd--
        }
        up = this.xEnd
        bp = this.xStart
        uq = this.zEnd
        bq = this.zStart
        break
      case 'z':
        if (this.zStart == this.zEnd) return
        if (reverse) {
          o = this.zStart
          this.zStart++
        } else {
          o = this.zEnd
          this.zEnd--
        }
        up = this.xEnd
        bp = this.xStart
        uq = this.yEnd
        bq = this.yStart
        break
    }
    for (let p = bp; p <= up; p++) {
      for (let q = bq; q <= uq; q++) {
        let obj = null
        switch (direction) {
          case 'x':
            obj = group.children[o].children[p].children[q]
            break
          case 'y':
            obj = group.children[p].children[o].children[q]
            break
          case 'z':
            obj = group.children[p].children[q].children[o]
            break
        }
        if (obj.visible == true) {
          obj.visible = false
          obj.floor = true
        }
      }
    }
    if(!mode.automatic()){
      this.sweeper.musicCtrl.playGameAudio({type:'hidden'})
    }
  }
  //层级显示
  floorShow(direction, reverse) {
    let difficulty = this.sweeper.difficulty
    let group = this.sweeper.threeManager.group
    if (!reverse) reverse = false
    let up,uq,bp,bq
    let o = 0
    switch (direction) {
      case 'x':
        if (reverse) {
          if(this.xStart==0)return
          this.xStart--
          o = this.xStart
        } else {
          if(this.xEnd==difficulty.ux-1)return
          this.xEnd++
          o = this.xEnd
        }
        up = this.yEnd
        bp = this.yStart
        uq = this.zEnd
        bq = this.zStart
        break
      case 'y':
        if (reverse) {
          if(this.yStart==0)return
          this.yStart--
          o = this.yStart
        } else {
          if(this.yEnd==difficulty.uy-1)return
          this.yEnd++
          o = this.yEnd
        }
        up = this.xEnd
        bp = this.xStart
        uq = this.zEnd
        bq = this.zStart
        break
      case 'z':
        if (this.zStart > this.zEnd) return
        if (reverse) {
          if(this.zStart==0)return
          this.zStart--
          o = this.zStart
        } else {
          if(this.zEnd==difficulty.uz-1)return
          this.zEnd++
          o = this.zEnd
        }
        up = this.xEnd
        bp = this.xStart
        uq = this.yEnd
        bq = this.yStart
        break
    }
    for (let p = bp; p <= up; p++) {
      for (let q = bq; q <= uq; q++) {
        let obj = null
        switch (direction) {
          case 'x':
            obj = group.children[o].children[p].children[q]
            break
          case 'y':
            obj = group.children[p].children[o].children[q]
            break
          case 'z':
            obj = group.children[p].children[q].children[o]
            break
        }
        if (obj.visible == false && obj.floor == true) {
          obj.visible = true
          obj.floor = false
        }
      }
    }
    this.sweeper.musicCtrl.playGameAudio({type:'hidden'})
  }
  //镜头复位
  refreshCamera() {
    this.sweeper.threeManager.controls.reset()
  }
  //魔棒功能
  btnHandleMagic() {
    let hiddenOnce = []
    let difficulty = this.sweeper.difficulty
    let group = this.sweeper.threeManager.group
    let numData = this.sweeper.numData
    let hiddenList = this.sweeper.hiddenList
    for (let x = 0; x < difficulty.ux; x++) {
      for (let y = 0; y < difficulty.uy; y++) {
        for (let z = 0; z < difficulty.uz; z++) {
          let mesh = group.children[x].children[y].children[z]
          if (mesh.visible == false) {
            //如果已经隐藏不归我管
            continue
          }
          if (mesh.know == false) {
            //如果未知不归我管
            continue
          }
          if (mesh.flag == true || numData[x][y][z] == -1) {
            //如果是雷或者插旗也不隐藏
            continue
          }
          if (
            getMesh(x + 1, y, z) &&
            getMesh(x + 1, y + 1, z) &&
            getMesh(x + 1, y - 1, z) &&
            getMesh(x + 1, y, z + 1) &&
            getMesh(x + 1, y, z - 1) &&
            getMesh(x - 1, y, z) &&
            getMesh(x - 1, y + 1, z) &&
            getMesh(x - 1, y - 1, z) &&
            getMesh(x - 1, y, z + 1) &&
            getMesh(x - 1, y, z - 1) &&
            getMesh(x, y + 1, z) &&
            getMesh(x, y - 1, z) &&
            getMesh(x, y, z + 1) &&
            getMesh(x, y, z - 1) &&
            getMesh(x, y + 1, z + 1) &&
            getMesh(x, y + 1, z - 1) &&
            getMesh(x, y - 1, z + 1) &&
            getMesh(x, y - 1, z - 1)
          ) {
            //如果该方块周围都已知则隐藏
            mesh.visible = false
            hiddenOnce.push(mesh)
          }
        }
      }
    }
    if (hiddenOnce.length > 0) {
      hiddenList.push(hiddenOnce)
      this.sweeper.musicCtrl.playGameAudio({type:'hidden'})
    }
    function getMesh(x, y, z) {
      if (x < 0 || x >= difficulty.ux || y < 0 || y >= difficulty.uy || z < 0 || z >= difficulty.uz) {
        //越界的判为已知
        return true
      }
      let obj = group.children[x].children[y].children[z]
      return obj.know || obj.flag
    }
  }
}
